<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- <add-dept :show-dialog="showDialog" @update:showDialog="closeDialog" /> -->
    <!-- .sync 语法糖   -->
    <add-dept ref="addDept" :title="title" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepart="getDepartment" />
    <!-- <add-dept :show-dialog="showDialog" @update:showDialog="showDialog=$event" /> -->
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null, // 存储当前点击的id// 显示模态框
      title: '添加部门',
      flax: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async getDepartment() {
      const result = await getDepartment()
      // 把后端返回的普通的数组数据 转成 树状数据
      this.depts = transListToTreeData(result.data, 0)
    },
    // 点击 下拉菜单选项
    async operateDept(type, id) {
      this.currentNodeId = id
      if (type === 'add') {
        // 显示模态框
        this.showDialog = true
        this.title = '添加部门'
      } else if (type === 'edit') {
        // 显示模态框
        this.showDialog = true
        this.title = '编辑部门'
        // 异步请求，会发默认值给子组件，nextTick 函数通常用于处理异步代码的执行顺序和优化性能
        this.$nextTick(() => this.$refs.addDept.getDepartmentDetail())
      } else if (type === 'del') {
        // 删除部门
        await this.$confirm('您确认要删除该部门吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        })
        await delDepartment(id)
        // 提示消息
        this.$message.success('删除部门成功')
        this.getDepartment()
      }
    }

  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 30px;
}
</style>
